package logic;

import guiDisplay.UserData2;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Set;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import data.MovieTuple;
import data.RatingTuple;
import data.SystemData;

public class Reports {
	
	private static final String PICTUER_HOME = "C:\\Downloads\\eclipse\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\MovieRecommender3\\reports\\";											
	private static final int MAXRATING = 5;
	
	public static String getMoviesChart(int n, boolean highest){
		MovieTuple[] movies = SystemData.getInstance().getNMovies(n, highest);
		DefaultPieDataset p = new DefaultPieDataset();
		int i=0;
		for (int j=0; j<movies.length; j++){
			if (i<n){
				p.insertValue(i, movies[i].get_name()+" : "+movies[i].get_raters(), movies[i].get_raters());
				i++;
			}
		}
		
		
		JFreeChart chart = ChartFactory.createPieChart("Movie rating chart", p, false, false, false);
	
		BufferedImage img = chart.createBufferedImage(400, 400+n*10);
//		JFrame frame = new JFrame();
//		JPanel panel = new JPanel(new GridLayout(1,1));
//		JLabel label = new JLabel();
//		label.setIcon(new ImageIcon(img));
//		panel.add(label);
//		frame.setContentPane(panel);
//		frame.pack();
//		frame.setVisible(true);
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		File f = new File(PICTUER_HOME +highest+n+".png");
		try {
			f.createNewFile();
			f.setExecutable(true);
			f.setWritable(true);
			f.setReadable(true);
			ImageIO.write(img, "png", f);
			
		} catch (IOException e){
			e.printStackTrace();
		}
		return "reports/"+highest+n+".png";
	}
	
	public static String getMovieRatingChart(String movie){
		Set<RatingTuple> ratings = SystemData.getInstance().getRatingsByMovie(movie);
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		int[] numRatings = new int[MAXRATING+1];
		for (int i = 0; i <= MAXRATING ; i++){
			numRatings[i]=0;
		}
		for (RatingTuple rating: ratings){
			numRatings[rating.get_rating()]++;
		}
		for (int i = 0; i <= MAXRATING ; i++){
			dataset.addValue(numRatings[i], "0", ""+i);
		}
		JFreeChart chart = ChartFactory.createBarChart("Rating distribution for movie " + movie,"rating","number of raters",dataset,PlotOrientation.VERTICAL,false,false,false);
		File f = new File(PICTUER_HOME +movie+".png");
		
		BufferedImage img = chart.createBufferedImage(400, 400);
		try {
			f.createNewFile();
			f.setExecutable(true);
			f.setWritable(true);
			f.setReadable(true);
			ImageIO.write(img, "png", f);
		} catch (IOException e){
			e.printStackTrace();
		}
		return "reports/"+movie+".png";
		
	}
	
	public static MovieTuple[] getMovies(int n, boolean highest){
		MovieTuple[] movies = SystemData.getInstance().getNMovies(n, highest);
		return movies;
	}
	
	public static UserData2[] getMovieRaters(String movie, int minRating){
		return SystemData.getInstance().getMovieRaters(movie, minRating);
	}
	
	public static MovieTuple[] getUserMovies(String user, int minRating){
		return SystemData.getInstance().getUserMovies(user, minRating);
	}
	
}
